<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            background: aquamarine;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>

</body>
<script>
    var box = document.getElementsByClassName('box')[0]
    box.onmousedown = function (e) {
        var e = e || window.event
        var x = e.pageX - box.offsetLeft
        var y = e.pageY - box.offsetTop
        console.log(x, y)
        document.onmousemove = function (e) {
            var X = e.pageX - x
            var Y = e.pageY - y
            box.style.left = X + 'px'
            box.style.top = Y + 'px'
        }
        box.onmouseup = function () {
            document.onmousemove = null
        }
    }

</script>

</html>